{% extends 'layout.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/me.css' %}">
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
{% endblock %}
{% block content %}
    <div style="float: right;width: 300px;">
        <form method="get">
            <div class="input-group">
                <input type="text" name="q" class="form-control" placeholder="Search for..."
                       value="{{ search_data }}">
                <span class="input-group-btn">
                <button class="btn btn-default" type="submit">
                    <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                </button>
              </span>
            </div>
        </form>
    </div>
    <div class="container">
        <div style="margin-bottom: 10px">
            <a class="btn btn-success" href="/me/">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                个人信息
            </a>

            <a class="btn btn-success" href="/me/fabu/article">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                文章
            </a>

            <a class="btn btn-success" href="/me/fabu/tiezi">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                帖子
            </a>

            <a class="btn btn-success" href="/me/pl">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                评论
            </a>

            <a class="btn btn-success" href="/me/likes/">
                <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
                喜欢
            </a>
        </div>
    </div>

        <div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-heading">
        <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
        用户列表
    </div>

    <!-- Table -->
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>标题</th>
            <th>发布时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for obj in queryset %}
            <tr>
                <td>{{ obj.like_id.title }}</td>
                <td>{{ obj.like_id.release_time|date:"Y-m-d" }}</td>
                <td>
                    <input uid="{{ obj.id }}" utitle="{{ obj.like_id.title }}" id="tiaozhuan" class="btn btn-primary btn-xs" type="button" value="打开">
                    <input uid="{{ obj.id }}" id="btn-delete" class="btn btn-danger btn-xs btn-delete" type="button" value="删除">
                </td>
            </tr>
            {% endfor %}

            </tbody>
        </table>
    </div>

        <!-- 删除（对话框） -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">

            <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <h4>是否确定删除？</h4>
                <p style="margin: 10px 0;"> 删除后，所有关联的相关数据都会被删除。</p>
                <p style="text-align: right;">
                    <button id="btnConfirmDelete" type="button" class="btn btn-danger">确 定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                </p>
            </div>

        </div>
    </div>
{% endblock %}
{% block js %}
   <script src="{% static '/js/jquery-3.6.0.min.js' %}"></script>
    <script src="{% static '/js/bootstrap.min.js' %}"></script>
    <script type="text/javascript">
    var DELETE_ID;

    $(function () {
        bindBtnDeleteEvent();
        bindBtnConfirmDeleteEvent();
        bintiaoEvent();
    })

    function bintiaoEvent(){
        $("#tiaozhuan").click(function () {
            window.location.href = '/article/1/read/'
        })
    }

    function bindBtnDeleteEvent() {
        $("#btn-delete").click(function () {
            // alert("点击了删除");
            // 显示删除对话框
            $("#deleteModal").modal('show');

            // 获取当前行的ID并赋值给全部变量。
            DELETE_ID = $(this).attr("uid");
        });
    }

    function bindBtnConfirmDeleteEvent() {
        $("#btnConfirmDelete").click(function () {
            $.ajax({
                url: "/me/likes/delete/",  //    => /order/delete/?uid=123
                type: "GET",
                data: {
                    uid: DELETE_ID
                },
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        // 隐藏删除框
                        // $("#deleteModal").modal('hide');
                        // 在页面上将当前一行数据删除（js）
                        // $("tr[uid='" + DELETE_ID + "']").remove();
                        // 要删除的ID制空
                        // DELETE_ID = 0;
                        // 简单的思路：
                        location.reload();
                    } else {
                        // 删除失败
                        alert(res.error);
                    }
                }
            })
        });
    }
</script>
{% endblock %}